<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="">
        </h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="请输入手机号"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >登录</el-button>

      <div class="tips">
        <span style="margin-right: 5px">还没有账号？</span>
        <a href="#">立即注册</a>
      </div>
    </el-form>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import { setToken } from '@/utils/auth'
import { LoginAPI } from '@/api/login'

export default {
  name: 'Login',
  data() {
    return {
      // 用户名密码
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      timerId: null,
      // 校验规则
      loginRules: {
        // required： true  必填
        mobile: [
          { required: true, trigger: 'blur', message: '手机号不能为空' },
          // pattern 正则校验
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            trigger: 'blur',
            message: '手机格式不正确'
          }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          { min: 6, trigger: 'blur', message: '密码不小于6位' }
        ]
      },
      loading: false,
      passwordType: 'password',
      // 记录用户需要跳转的路径
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async handleLogin() {
      // 每次点击登录清除定时器
      if (this.timerId != null) clearTimeout(this.timerId)

      // 防抖
      this.timerId = setTimeout(
        () => {
          // validate() 校验表单中的所有字段
          this.$refs.loginForm.validate(async(valid) => {
            if (valid) {
              // 调用actions 配置项方法，发送请求，保存token
              // 这里是异步操作，让await同步化
              await this.$store.dispatch('user/setTokenActions', {
                loginForm: this.loginForm
              })

              // 获取用户数据
              await this.$store.dispatch('user/setUserInfoActions')
              // 提示
              this.$message({
                message: '登录成功！',
                type: 'success'
              })
              // 跳转至首页 (this.redirect 记录着用户从那个页面来的路由路径)
              this.$router.push(this.redirect || '/')
            } else {
              // 表单校验失败
              this.$message('请填写手机号或密码！')
            }
          })
        },
        // 如果是第一次，定时器延迟为0
        this.timerId == null ? 0 : 1000
      )
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #d5e5ff;
$light_gray: #5f94a8;
$cursor: #5f94a8;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        // input 框内填充色
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item__error {
    color: rgb(230, 248, 255);
  }
  // 登录按钮颜色
  .el-button--primary {
    opacity: 0.8;
    border: none;
    background-image: linear-gradient(to right, #acb6e5, #86fde8);
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: $bg;
    border-radius: 5px;
    opacity: 0.8;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('~@/assets/common/login02.jpg') no-repeat center;
  background-size: 100% 100%;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    text-align: center;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
